import {listPost} from "@/api/forum/post"
import useUserStore from "@/store/modules/user"
import {defineStore} from 'pinia'

const usePostStore = defineStore(
  'post',
  {
    state: () => ({
        changeTab: '1',
        postList: [],
        queryParams: {
          title: "",
          pageNum: 1,
          pageSize: 4,
          orderByColumn: "create_time",
          isAsc: "desc",
          schoolId: null,
          collegeId: null,
          specialtyId: null
        },
        sortList: [
          {
            value: "最新更新",
            key: "create_time"
          },
          {
            value: "收藏最多",
            key: "collect_number"
          },
          {
            value: "点赞最多",
            key: "upvote_number"
          },
          {
            value: "评论最多",
            key: "review_number"
          },
        ],
        total: 0
    }),
    actions: {
      //设置排序方式
      setSort(key){
        this.queryParams.orderByColumn = key;
      },
      getSortValueForKey(key){
        for(var i=0;i<this.sortList.length;i++){
          if(key==this.sortList[i].key){
            return this.sortList[i].value;
          }
        }
      },
      getSortKeyForValue(value){
        for(var i=0;i<this.sortList.length;i++){
          if(value==this.sortList[i].value){
            return this.sortList[i].key;
          }
        }
      },
      // 获取帖子数据
      getPostList() {
        return new Promise((resolve, reject) => {
            // console.log(this.queryParams)
            // this.queryParams.pageSize = 4;
            listPost(this.queryParams).then(res => {
              this.postList = res.rows
              this.total = res.total
              resolve()
            }).catch(error => {
              reject(error)
            })
        })
      },
      onLoadPostList() {
        return new Promise((resolve, reject) => {
            // console.log(this.queryParams)
            this.queryParams.pageNum = this.queryParams.pageNum +1;
            // this.queryParams.pageSize = 2;
            listPost(this.queryParams).then(res => {
              if(res.code==200){
                res.rows.forEach(post => {
                  this.postList.push(post);
                });
                this.total = res.total
              }
                resolve()
            }).catch(error => {
              reject(error)
            })
        })
      },
      //查询本校论坛帖子-替换查询参数
      resetQueryForLocal(){
        this.queryParams = {
          title: "",
          pageNum: 1,
          pageSize: 7,
          orderByColumn: "create_time",
          isAsc: "desc",
          schoolId: useUserStore().schoolId,
          collegeId: null,
          specialtyId: null
        }
      },
      //查询全国论坛帖子-替换查询参数
      resetQueryForAll(){
        this.queryParams = {
          title: "",
          pageNum: 1,
          pageSize: 7,
          orderByColumn: "create_time",
          isAsc: "desc",
          schoolId: null,
          collegeId: null,
          specialtyId: null
        }
      },
      //查询自己的帖子-替换查询参数
      resetQueryForSelf(){
        this.queryParams = {
          userId: useUserStore().id,
          title: "",
          pageNum: 1,
          pageSize: 4,
          orderByColumn: "create_time",
          isAsc: "desc",
          schoolId: null,
          collegeId: null,
          specialtyId: null
        }
      },
      //查询自己收藏的帖子-替换查询参数
      resetQueryForCollect(){
        this.queryParams = {
          // userId: useUserStore().id,
          title: "",
          pageNum: 1,
          pageSize: 4,
          orderByColumn: "create_time",
          isAsc: "desc",
          schoolId: null,
          collegeId: null,
          specialtyId: null,
          queryType: 1,
          queryId: useUserStore().id
        }
      },
      //查询自己点赞的帖子-替换查询参数
      resetQueryForUpvote(){
        this.queryParams = {
          title: "",
          pageNum: 1,
          pageSize: 4,
          orderByColumn: "create_time",
          isAsc: "desc",
          schoolId: null,
          collegeId: null,
          specialtyId: null,
          queryType: 2,
          queryId: useUserStore().id
        }
      },
      //将数字规范化后输出（超过万的加工万字，超过亿的加个亿字）
      normalizeNumber(number){
        // if(! number instanceof Number){
        //   number = Number.parseInt(number);
        // }
        // console.log("数字：",number )
        // console.log(typeof number )
        number = Number.parseInt(number);
        
        // console.log(typeof number )
        if(number>100000000){
          return parseInt(number/100000000) + "亿";
        }else if(number>10000){
          return parseInt(number/10000) + "万";
        }else{
          return number;
        }
      },
    }
  })

export default usePostStore